package org.tech4j.springboot.starter.distributedlock.redisson;

import org.redisson.api.RedissonClient;
import org.redisson.spring.starter.RedissonAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.tech4j.springboot.starter.distributedlock.api.ILock;
import org.tech4j.springboot.starter.distributedlock.api.util.LockUtils;

/**
 * @program: tech4j
 * @description:
 * @author: YuKai Fan
 * @create: 2023/2/19 22:39
 **/
@Configuration
@AutoConfigureAfter(RedissonAutoConfiguration.class)
public class RedissonLockConfiguration {

    @Bean
    @ConditionalOnBean(RedissonClient.class)
    @ConditionalOnMissingBean(ILock.class)
    public ILock redissonLock(RedissonClient redissonClient) {
        RedissonLock redissonLock = new RedissonLock(redissonClient);
        LockUtils.setLock(redissonLock);
        return redissonLock;
    }
}
